package servlets;


import java.io.*;
import java.util.List;


import javax.servlet.*;
import javax.servlet.http.*;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.jdom.output.XMLOutputter;


import util.*;



/**
 * Servlet implementation class ProcessRequest
 */
public class UpdateComment extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateComment() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	XMLOutputter outter = new XMLOutputter();
    	Namespace def = Namespace.getNamespace("http://www.w3.org/2005/Atom");
        String feedlink = request.getParameter("feedlink");
        String entrylink = request.getParameter("entrylink");
        String comment = request.getParameter( "comment" );
        System.out.println("Do the comment stuff " + feedlink + " " + entrylink + " " + comment);
        //get whole feed
        Document dom = null;
        try {
            dom = AtomPublisher.getFromEXist( feedlink );
        } catch( Exception e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("The first get: " + outter.outputString(dom));
        Element entry = null;
        @SuppressWarnings("rawtypes")
		List nodes = dom.getRootElement().getChildren("entry", def);
        for(Object o : nodes){
        	Element ent = (Element)o;
        	if(o instanceof Element){
        		System.out.println("in entry");
        		@SuppressWarnings("rawtypes")
				List links = ent.getChildren("link", def);
        		for(Object o1 : links){
        			if(o1 instanceof Element){
        				Element el = (Element) o1;
        				System.out.println("href = " + el.getAttribute("href").getValue());
        				if(el.getAttribute("href").getValue().equals(entrylink)){
        					entry = ent;
        				}
        			}
        		}
        	}
        }
        
        //add the comment to entry
        Namespace customNS = Namespace.getNamespace("ns1", "http://www.example.org");
        Namespace ns1 = Namespace.getNamespace("http://www.example.org");
        Element usercomment= new Element("usercomment",customNS);
        usercomment.addContent( comment );
        
        System.out.println("Getting entry info");
        for(Object o : entry.getChildren()){
        	if(o instanceof Element){
        		Element e = (Element) o;
        		System.out.println("Name: " + e.getName() + " NS: " + e.getNamespaceURI());
        		
        	}
        }
        
        Element contentElement = entry.getChild("content", def);
        System.out.println("the content " + contentElement);
        Element pairingElement = contentElement.getChild("pairing", ns1);
        pairingElement.addContent( usercomment );
        
        Element newEntry = new Element("entry", def);
        newEntry.addContent(entry.cloneContent());
        Document entrydom = new Document(newEntry);
        
        System.out.print("OUTPUT: " + outter.outputString(entrydom));
        //put the results
        try {
            AtomPublisher.putToEXist( feedlink + entrylink, entrydom );
        } catch( Exception e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
//        //get, redirect
        Document returnDom = null;
        try {
            returnDom = AtomPublisher.getFromEXist( feedlink );
        } catch( Exception e ) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        request.setAttribute("xmlDom", returnDom);
        request.setAttribute("link", feedlink);
        RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
        dispatcher.forward(request, response);
        
        
    }
    
    
    public void testPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    
    }

}
